package com.syh.jn.scm.domain.supplier.entity.system;

import cn.redscarf.commons.util.bean.ExtBeanUtils;
import cn.redscarf.commons.util.other.ExtAssert;
import cn.redscarf.commons.spring.SpringContextUtils;
import cn.redscarf.commons.util.validator.ValidationUtils;
import cn.redscarf.commons.wrapper.Restrictions;
import cn.redscarf.commons.wrapper.Wrapper;
import com.syh.jn.scm.domain.supplier.repository.system.SupplierAccountRepository;
import com.syh.jn.scm.domain.supplier.repository.system.SupplierMenuRepository;
import com.syh.jn.scm.domain.supplier.repository.system.SupplierRoleRepository;
import com.syh.jn.scm.domain.support.SuperBusinessAggregate;
import com.syh.jn.scm.dto.operation.system.permission.PermissionVo;
import com.syh.jn.scm.dto.supplier.system.supplierRole.SupplierRoleMto;
import com.syh.jn.scm.dto.supplier.system.supplierRole.SupplierRoleSto;
import com.syh.jn.scm.exception.BusinessException;
import lombok.Data;
import lombok.EqualsAndHashCode;
import lombok.NoArgsConstructor;

import java.util.Collections;
import java.util.List;

@Data
@NoArgsConstructor
@EqualsAndHashCode(callSuper = true)
public class SupplierRole extends SuperBusinessAggregate<Long> {


	public SupplierRole(SupplierRoleSto sto) {
		ExtAssert.notNull(sto, "对象为空");
		ValidationUtils.validate(sto);
		this.terminalType = "SUPPLIER";
		this.createTimeStamp = System.currentTimeMillis();
		ExtBeanUtils.copyProperties(sto, this, false);
	}

	public void modify(SupplierRoleMto mto) {
		ExtAssert.notNull(mto, "对象为空");
		ValidationUtils.validate(mto);
		this.lastUpdateTimeStamp = System.currentTimeMillis();
		ExtBeanUtils.copyProperties(mto, this, false);
	}

	public void updateMenus(List<Long> menuIds) {
		if (menuIds.isEmpty()) {
			this.menuList = Collections.emptyList();
			return;
		}
		SupplierMenuRepository supplierMenuRepository = (SupplierMenuRepository) SpringContextUtils.getBean(SupplierMenuRepository.class);
		List<SupplierMenu> menuList = supplierMenuRepository.getList(Wrapper.build().append(Restrictions.in("id", menuIds.toArray())));
		this.menuList = menuList;
	}

	/**
	 * 数据删除
	 */
	public void destroy() {
		SupplierRoleRepository supplierRoleRepository = (SupplierRoleRepository) SpringContextUtils.getBean(SupplierRoleRepository.class);
		List<PermissionVo> permission = supplierRoleRepository.getPermissionByRoleId(id);
		if (permission != null && permission.size() > 0) {
			throw new BusinessException("该角色存在菜单,不能删除");
		}
		SupplierAccountRepository accountRepository = (SupplierAccountRepository) SpringContextUtils.getBean(SupplierAccountRepository.class);
		Wrapper wrapper = Wrapper.build();
		wrapper.append(
						Restrictions.and(Restrictions.eq("roleId", id))
		);
		List accounts = accountRepository.findListPage(wrapper, 0, 1000);
		if (accounts != null && accounts.size() > 0) {
			throw new BusinessException("角色不能删除,已有账号正在使用");
		}
	}

	/**
	 * 角色名称
	 */
	private String roleName;
	private String terminalType;
	private String description;
	private Integer orderLevel;
	private List<SupplierMenu> menuList;
	private Long businessEntityId;
}
